//
//  DolphinUICategory.m
//  Dolphin
//
//  Created by Matthew on 11-7-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DolphinUICategory.h"


@implementation DolphinUICategory

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) 
    {        
        // Initialization code
        bgLayer = [[UIView alloc] initWithFrame:frame];
        bgLayer.frame = CGRectMake(0, 0,frame.size.width,1024);//frame.size.height);
        bgLayer.backgroundColor = [UIColor blackColor];
        bgLayer.alpha = .5f;
        [self addSubview:bgLayer];
     
        uiscrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        uiscrollView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        uiscrollView.delegate = self;
		uiscrollView.showsHorizontalScrollIndicator = NO;
		uiscrollView.showsVerticalScrollIndicator = YES;
        uiscrollView.contentSize = frame.size;
        uiscrollView.minimumZoomScale = 1;
        uiscrollView.maximumZoomScale = 1;
        uiscrollView.zoomScale = 1;
        [self addSubview:uiscrollView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:@"img_load_over" object:Nil];
        
    }
    return self;
}

-(void) initWithAlbumList:(AlbumXMLParser *) inAlbumXMLParser
                     orientation:(UIInterfaceOrientation) interfaceOrientation;
{
    NSMutableArray *unitsArr = inAlbumXMLParser.albumIconArr;
    NSMutableArray *titleArr = inAlbumXMLParser.albumTitleArr;
    NSMutableArray *albumArr = inAlbumXMLParser.albumPathArr;
    
    unit_width = inAlbumXMLParser.unit_width;
    unit_height = inAlbumXMLParser.unit_height;
    unit_between_width = inAlbumXMLParser.unit_between_width;
    unit_between_height = inAlbumXMLParser.unit_between_height;
    unit_start_off_x = inAlbumXMLParser.unit_start_off_x;
    unit_start_off_y = inAlbumXMLParser.unit_start_off_y;
    border_width = inAlbumXMLParser.border_width;
    border_height = inAlbumXMLParser.border_height;
    
    //get screen size
    CGRect screen_rect = [[UIScreen mainScreen] bounds];
    col_num = floor((screen_rect.size.width-unit_between_width-10)/(unit_width+border_width+unit_between_width));
    row_num = ceil([unitsArr count]/col_num);
    unit_num = [unitsArr count];
    
    float unit_width_adjusted = (screen_rect.size.width-unit_between_width-10)/col_num;
    
    for (int i=0; i<[unitsArr count]; i++) 
    {
        int pos_x = unit_start_off_x+unit_between_width+(i%col_num)*(unit_width_adjusted);
        int pos_y = unit_start_off_y+floor(i/col_num)*(unit_height+unit_between_height);
        
        UICategoryUnit *uicategoryUnit = [[UICategoryUnit alloc] initWithFrame:CGRectMake(pos_x,pos_y,unit_width, unit_height)];
        [uicategoryUnit initUnit:[unitsArr objectAtIndex:i] title:[titleArr objectAtIndex:i] albumPath:[albumArr objectAtIndex:i] 
                                 borderWidth:border_width borderHeight:border_height fontSize:inAlbumXMLParser.font_size];
        [uiscrollView addSubview:uicategoryUnit];
    }
    
    uiscrollView.contentSize = CGSizeMake(self.frame.size.width,unit_start_off_y+(row_num+2)*(unit_height+unit_between_height));
    
    bgLayer.frame = CGRectMake(0, 0,uiscrollView.contentSize.width,uiscrollView.contentSize.height);
    bgLayer.backgroundColor = [UIColor blackColor];
    bgLayer.alpha = .5f;
    
    [self layoutAdjustWithOrientation:interfaceOrientation];
}

-(void) refreshPage
{
    [uiscrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}

-(void) notificationSelector:(NSNotification *) notification
{
    NSString *curNotificationName = [notification name];
    
    if([curNotificationName isEqualToString:@"img_load_over"])
    {
        if ([[[notification object] superview] isKindOfClass:[UICategoryUnit class]]) 
        {
            UICategoryUnit *category_unit =(UICategoryUnit *) [[notification object] superview];
            [category_unit layoutAdjust];
        }
    }
}

-(void) layoutAdjustWithOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[uiscrollView subviews] count]>=1 && unit_num>= 1) 
    {
        //get screen size
        CGRect screen_rect = [[UIScreen mainScreen] bounds];
        
        if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
           interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
        {
            screen_rect.size.width = [[UIScreen mainScreen] bounds].size.height;
            screen_rect.size.height = [[UIScreen mainScreen] bounds].size.width;
        }
        
        col_num = floor((screen_rect.size.width-unit_between_width-10)/(unit_width+border_width+unit_between_width));
        row_num = ceil(unit_num/col_num);

        float unit_width_adjusted = (screen_rect.size.width-unit_between_width-10)/col_num;
        int i = 0;
        
        for (UICategoryUnit *singleView in[uiscrollView subviews]) 
        {
            if (singleView != Nil && [singleView isKindOfClass:[UICategoryUnit class]]) 
            {
                int pos_x = unit_start_off_x+unit_between_width+(i%col_num)*(unit_width_adjusted);
                int pos_y = unit_start_off_y+floor(i/col_num)*(unit_height+unit_between_height);
                
                singleView.frame = CGRectMake(pos_x, pos_y, singleView.frame.size.width, singleView.frame.size.height);
                
                i++;
            }
            
        }
        
        uiscrollView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        uiscrollView.contentSize = CGSizeMake(self.frame.size.width,unit_start_off_y+(row_num+2)*(unit_height+unit_between_height));
        
        bgLayer.frame = CGRectMake(0, 0,uiscrollView.contentSize.width,uiscrollView.contentSize.height);
        bgLayer.backgroundColor = [UIColor blackColor];
        bgLayer.alpha = .5f;
    }
}

-(void) arrangeUnitsPositions
{
    
}

-(void) reset
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    for (UICategoryUnit*singleView in[uiscrollView subviews]) 
    {
        [singleView removeFromSuperview];
        if (singleView != Nil && [singleView isKindOfClass:[UICategoryUnit class]]) 
        {
            [singleView reset];
        }
        [singleView release];
    }
}

- (void)dealloc
{
    [super dealloc];
}

@end
